@echo off
chcp 65001 >nul
echo ==========================================
echo AIOT智能学习桌项目 - Windows开发环境安装
echo ==========================================
echo.

:: 检查管理员权限
net session >nul 2>&1
if %errorLevel% == 0 (
    echo ✓ 检测到管理员权限
) else (
    echo ❌ 需要管理员权限运行此脚本
    echo 请右键选择"以管理员身份运行"
    pause
    exit /b 1
)

:: 创建必要的目录
echo 📁 创建项目目录结构...
if not exist "build" mkdir build
if not exist "logs" mkdir logs
if not exist "data" mkdir data
if not exist "user_profiles" mkdir user_profiles
if not exist ".vscode" mkdir .vscode
echo ✓ 目录创建完成

:: 检查Python安装
echo.
echo 🐍 检查Python环境...
python --version >nul 2>&1
if %errorLevel% == 0 (
    echo ✓ Python已安装
    python --version
) else (
    echo ❌ Python未安装
    echo 请访问 https://www.python.org/downloads/windows/ 下载安装Python
    echo 安装时请勾选"Add Python to PATH"
    pause
    exit /b 1
)

:: 检查pip
pip --version >nul 2>&1
if %errorLevel% == 0 (
    echo ✓ pip已安装
) else (
    echo ❌ pip未安装，正在安装...
    python -m ensurepip --upgrade
)

:: 创建Python虚拟环境
echo.
echo 🔧 创建Python虚拟环境...
if exist ".venv" (
    echo ✓ 虚拟环境已存在
) else (
    python -m venv .venv
    echo ✓ 虚拟环境创建完成
)

:: 激活虚拟环境并安装依赖
echo.
echo 📦 安装Python依赖包...
call .venv\Scripts\activate.bat
python -m pip install --upgrade pip
pip install -r requirements.txt
if %errorLevel% == 0 (
    echo ✓ Python依赖安装完成
) else (
    echo ❌ Python依赖安装失败
    echo 请检查requirements.txt文件和网络连接
)

:: 安装Windows特定依赖
echo.
echo 🪟 安装Windows特定依赖...
pip install pywin32 wmi
echo ✓ Windows依赖安装完成

:: 检查ARM工具链
echo.
echo 🔧 检查ARM开发工具链...
arm-none-eabi-gcc --version >nul 2>&1
if %errorLevel% == 0 (
    echo ✓ ARM工具链已安装
    arm-none-eabi-gcc --version
) else (
    echo ❌ ARM工具链未安装
    echo.
    echo 请按以下步骤安装ARM工具链：
    echo 1. 访问 https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
    echo 2. 下载 gcc-arm-none-eabi-10.3-2021.10-win32.exe
    echo 3. 运行安装程序，勾选"Add path to environment variable"
    echo 4. 重新运行此脚本
    echo.
)

:: 检查Make工具
echo.
echo 🛠️ 检查Make工具...
make --version >nul 2>&1
if %errorLevel% == 0 (
    echo ✓ Make工具已安装
) else (
    echo ❌ Make工具未安装
    echo.
    echo 安装Make工具的方法：
    echo 方法一：使用Chocolatey
    echo   1. 安装Chocolatey: https://chocolatey.org/install
    echo   2. 运行: choco install make
    echo.
    echo 方法二：使用MSYS2
    echo   1. 安装MSYS2: https://www.msys2.org/
    echo   2. 在MSYS2终端运行: pacman -S make
    echo.
    echo 方法三：下载MinGW-w64
    echo   访问: https://www.mingw-w64.org/downloads/
    echo.
)

:: 检查ESWIN IDE
echo.
echo 🎯 检查ESWIN开发环境...
if exist "C:\Program Files\ESWIN\IDE" (
    echo ✓ ESWIN IDE已安装
) else (
    echo ❌ ESWIN IDE未安装
    echo.
    echo 请按以下步骤安装ESWIN IDE：
    echo 1. 访问奕斯伟官网: https://www.eswin.com/
    echo 2. 注册开发者账号
    echo 3. 下载ESWIN IDE和EAM2011 SDK
    echo 4. 联系技术支持获取下载链接
    echo 5. 安装完成后重新运行此脚本
    echo.
)

:: 检查CH347驱动
echo.
echo 🔌 检查CH347调试器驱动...
driverquery | findstr "CH34" >nul 2>&1
if %errorLevel% == 0 (
    echo ✓ CH347驱动已安装
) else (
    echo ❌ CH347驱动未安装
    echo.
    echo 请按以下步骤安装CH347驱动：
    echo 1. 访问: https://www.wch.cn/downloads/CH341PAR_EXE.html
    echo 2. 下载 CH341PAR.EXE
    echo 3. 右键"以管理员身份运行"
    echo 4. 按照向导完成安装
    echo 5. 连接EBP3901开发板验证
    echo.
)

:: 检查ARM工具链（备用）
echo.
echo 🔧 检查ARM开发工具链（备用）...
arm-none-eabi-gcc --version >nul 2>&1
if %errorLevel% == 0 (
    echo ✓ ARM工具链已安装（备用编译环境）
    arm-none-eabi-gcc --version | head -1
) else (
    echo ⚠️ ARM工具链未安装（可选）
    echo.
    echo 如需备用编译环境，请安装ARM工具链：
    echo 1. 访问 https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
    echo 2. 下载 gcc-arm-none-eabi-10.3-2021.10-win32.exe
    echo 3. 运行安装程序，勾选"Add path to environment variable"
    echo.
)

:: 检查OpenOCD（备用调试工具）
echo.
echo 🔍 检查OpenOCD调试工具（备用）...
openocd --version >nul 2>&1
if %errorLevel% == 0 (
    echo ✓ OpenOCD已安装（备用调试环境）
    openocd --version | head -1
) else (
    echo ⚠️ OpenOCD未安装（可选）
    echo.
    echo 如需备用调试环境，请安装OpenOCD：
    echo 1. 访问: https://gnutoolchains.com/arm-eabi/openocd/
    echo 2. 下载最新版本: openocd-20250710.7z
    echo 3. 解压到: C:\OpenOCD\
    echo 4. 添加到PATH: C:\OpenOCD\bin
    echo.
)

:: 检查J-Link
JLink.exe -? >nul 2>&1
if %errorLevel% == 0 (
    echo ✓ J-Link已安装
) else (
    echo ❌ J-Link未安装
    echo 下载地址: https://www.segger.com/downloads/jlink/
)

:: 创建VSCode配置文件
echo.
echo ⚙️ 创建VSCode配置文件...

:: 创建settings.json
echo { > .vscode\settings.json
echo     "C_Cpp.default.compilerPath": "arm-none-eabi-gcc", >> .vscode\settings.json
echo     "C_Cpp.default.cStandard": "c99", >> .vscode\settings.json
echo     "C_Cpp.default.cppStandard": "c++17", >> .vscode\settings.json
echo     "C_Cpp.default.includePath": [ >> .vscode\settings.json
echo         "${workspaceFolder}/**", >> .vscode\settings.json
echo         "${workspaceFolder}/硬件适配层" >> .vscode\settings.json
echo     ], >> .vscode\settings.json
echo     "files.associations": { >> .vscode\settings.json
echo         "*.h": "c", >> .vscode\settings.json
echo         "*.c": "c" >> .vscode\settings.json
echo     }, >> .vscode\settings.json
echo     "python.defaultInterpreterPath": "./.venv/Scripts/python.exe" >> .vscode\settings.json
echo } >> .vscode\settings.json

:: 创建tasks.json
echo { > .vscode\tasks.json
echo     "version": "2.0.0", >> .vscode\tasks.json
echo     "tasks": [ >> .vscode\tasks.json
echo         { >> .vscode\tasks.json
echo             "label": "Build Project", >> .vscode\tasks.json
echo             "type": "shell", >> .vscode\tasks.json
echo             "command": "make", >> .vscode\tasks.json
echo             "args": ["all"], >> .vscode\tasks.json
echo             "group": { >> .vscode\tasks.json
echo                 "kind": "build", >> .vscode\tasks.json
echo                 "isDefault": true >> .vscode\tasks.json
echo             }, >> .vscode\tasks.json
echo             "presentation": { >> .vscode\tasks.json
echo                 "echo": true, >> .vscode\tasks.json
echo                 "reveal": "always", >> .vscode\tasks.json
echo                 "focus": false, >> .vscode\tasks.json
echo                 "panel": "shared" >> .vscode\tasks.json
echo             } >> .vscode\tasks.json
echo         }, >> .vscode\tasks.json
echo         { >> .vscode\tasks.json
echo             "label": "Clean Project", >> .vscode\tasks.json
echo             "type": "shell", >> .vscode\tasks.json
echo             "command": "make", >> .vscode\tasks.json
echo             "args": ["clean"] >> .vscode\tasks.json
echo         }, >> .vscode\tasks.json
echo         { >> .vscode\tasks.json
echo             "label": "Flash Program", >> .vscode\tasks.json
echo             "type": "shell", >> .vscode\tasks.json
echo             "command": "make", >> .vscode\tasks.json
echo             "args": ["flash"] >> .vscode\tasks.json
echo         } >> .vscode\tasks.json
echo     ] >> .vscode\tasks.json
echo } >> .vscode\tasks.json

echo ✓ VSCode配置文件创建完成

:: 创建快速启动脚本
echo.
echo 🚀 创建快速启动脚本...

:: Python仿真启动脚本
echo @echo off > start_simulator.bat
echo call .venv\Scripts\activate.bat >> start_simulator.bat
echo python run.py --mode simulator >> start_simulator.bat
echo pause >> start_simulator.bat

:: 语音测试启动脚本
echo @echo off > start_voice_test.bat
echo call .venv\Scripts\activate.bat >> start_voice_test.bat
echo python run.py --mode voice >> start_voice_test.bat
echo pause >> start_voice_test.bat

:: 坐姿检测启动脚本
echo @echo off > start_posture_test.bat
echo call .venv\Scripts\activate.bat >> start_posture_test.bat
echo python run.py --mode posture >> start_posture_test.bat
echo pause >> start_posture_test.bat

echo ✓ 快速启动脚本创建完成

:: 验证安装
echo.
echo 🔍 验证安装结果...
echo ==========================================

:: 验证Python环境
call .venv\Scripts\activate.bat
echo Python版本:
python --version

echo.
echo 已安装的Python包:
pip list | findstr -i "opencv mediapipe numpy speech"

:: 验证ARM工具链
echo.
arm-none-eabi-gcc --version >nul 2>&1
if %errorLevel% == 0 (
    echo ARM工具链版本:
    arm-none-eabi-gcc --version | head -1
) else (
    echo ❌ ARM工具链未正确安装
)

:: 显示下一步操作
echo.
echo ==========================================
echo 🎉 开发环境检查完成！
echo ==========================================
echo.
echo 📋 下一步操作：
echo.
echo 🔴 第一优先级 - 获取官方软件：
echo    1. 访问奕斯伟官网: https://www.eswin.com/
echo    2. 联系技术支持获取ESWIN IDE和SDK
echo    3. 下载并安装CH347驱动
echo    4. 验证EBP3901开发板连接
echo.
echo 🟡 第二优先级 - Python仿真测试：
echo    - 双击 start_simulator.bat 运行完整仿真
echo    - 双击 start_voice_test.bat 测试语音功能
echo    - 双击 start_posture_test.bat 测试坐姿检测
echo.
echo 🟢 第三优先级 - 硬件开发：
echo    - 使用ESWIN IDE创建项目
echo    - 基于SDK示例代码开发
echo    - 连接硬件模块进行测试
echo.
echo 🔵 备用方案 - 独立编译环境：
echo    - 使用ARM GCC + OpenOCD + VSCode
echo    - 手动编译和调试
echo    - 适合高级用户
echo.
echo 📞 获取帮助：
echo    - 奕斯伟技术支持（官方软件）
echo    - 参考"Windows软件安装完整清单.md"
echo    - 参考"EBP3901硬件连接详细指南.md"
echo.

:: 询问是否打开项目文件夹
set /p choice="是否在VSCode中打开项目？(y/n): "
if /i "%choice%"=="y" (
    code .
)

echo.
echo 按任意键退出...
pause >nul